using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using Org.Eurekaa.DNJ.Helpers;
using Org.Eurekaa.DNJ.UI.DNJSettings;
using Org.Eurekaa.DNJ.Settings;

namespace Org.Eurekaa.DNJ.UI.WebControls
{

    [ToolboxBitmap(typeof(Org.Eurekaa.DNJ.UI.Resources.icons.DNJUIIcons), "DNJTabs.bmp")]
    [ToolboxData("<{0}:DNJTabs runat=\"server\" Width=\"100%\" ></{0}:DNJTabs>")]
    [Designer(typeof(Org.Eurekaa.DNJ.UI.Design.WebControls.DNJTabsControlDesigner))]
    public class DNJTabs : DNJMultiboxControl
    {
         private DNJTabsSettings _settings = new DNJTabsSettings();

        [DefaultValue(""), Category("$.DNJ Settings"),DisplayName("Settings")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJTabsSettings Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }

        private DNJTabsEvents _events = new DNJTabsEvents();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Events")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJTabsEvents ClientEvents
        {
            get { return _events; }
            set { _events = value; }
        }

        public override void SaveSettings(ref object stateObject)
        {
            object[] lstObj = new object[2];
            lstObj[0] = Settings;
            lstObj[1] = ClientEvents;

            stateObject = lstObj;
            GenericSettingsObject allSettings = new GenericSettingsObject();


            PageHelper.AppendJQueryScript(ClientEvents.GetDefaultEventsScript(), this.Page.Header);

            
            Settings.PrepareJSON();   
            ClientEvents.PrepareJSON();

            allSettings.Append(Settings);
            allSettings.Append(ClientEvents);

            //stateObject = Settings;
            PageHelper.AppendJQueryPlugin("#" + this.ClientID, "tabs", allSettings.ToString(), this.Page.Header);
        }
        public override void LoadSettings(object stateObject)
        {
            //Settings = (stateObject as DNJTabsSettings);
            object[] lstObj = (object[])stateObject;
            Settings = (lstObj[0] as DNJTabsSettings);
            ClientEvents = (lstObj[1] as DNJTabsEvents);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            

            HtmlGenericControl ul = new HtmlGenericControl("ul");            
           
            int i = 0;
            foreach (DNJBox tabPage in BoxList)
            {
                HtmlGenericControl li = new HtmlGenericControl("li"); //The tab

                HtmlAnchor a = new HtmlAnchor();
                HtmlGenericControl spanTitle = new HtmlGenericControl("span"); //The title container
                spanTitle.InnerText = tabPage.Title;
                a.Controls.Add(spanTitle);

                HtmlGenericControl div = null;
                if ((tabPage.Ajax && !String.IsNullOrEmpty(tabPage.ContentUrl))) 
                {
                    a.HRef = tabPage.ContentUrl;  //ajax content doesn't need a content div
                    
                }
                else
                {
                    a.HRef = "#" + this.ClientID + tabPage.ClientID;
                    div = new HtmlGenericControl("div");
                    div.Attributes.Add("id", this.ClientID + tabPage.ClientID);
                }



                if (DesignMode)
                {
                    li.Attributes.Add("class", "ui-corner-top ui-state-default");
                    li.Attributes.Add("style", "list-style:none;margin:0px 2px;border:1px solid black;float:left;height:30px;position:relative;padding:0px");
                    //li.Style.Add("left", (i * 75).ToString() + "px");

                    a.Attributes.Add("style", "margin:0px;padding:0px");
                    //a.Style.Add("left", ((i * 75)).ToString() + "px");

                    if (div != null) div.Visible = false;
                    
                }

                li.Controls.Add(a);
                ul.Controls.Add(li);

                if (tabPage.Content != null && div != null)
                    tabPage.Content.InstantiateIn(div);

                if (div != null) Controls.Add(div);
                i++;
            }


            Controls.AddAt(0, ul);
            if (DesignMode)
            {
                ul.Attributes.Add("class", "ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all");
                ul.Attributes.Add("style", "list-style:none;margin:0px;padding:0px;position:relative;height:35px");

                HtmlGenericControl pdiv = new HtmlGenericControl("div");
                pdiv.Attributes.Add("class", "ui-tabs-panel ui-widget-content ui-corner-bottom");
                pdiv.Style.Add("padding", "10px");
                pdiv.Style.Add("height", (new Unit(this.Height.Value - 52.0)).ToString());
                Controls.AddAt(1, pdiv);
            }
        }

    }
}
